package com.study.talk.app.launcher;


import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;

import com.study.talk.app.login.LoginActivity;
import com.study.talk.R;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.netty.NettyService;
import com.study.talk.util.AppExecutor;
import com.study.talk.util.Constants;
import com.study.talk.util.OtherMessage;
import com.study.talk.util.alert.AlertUtil;

import org.greenrobot.eventbus.Subscribe;

public class StartActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        {
            String channelId = "畅聊好友消息"; //通知渠道的标识符
            String channelName = "好友消息通知";
            String channelDescription = "来自好友的消息";
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription(channelDescription);
            manager.createNotificationChannel(notificationChannel);
        }

        {
            String channelId = "畅聊群聊消息"; //通知渠道的标识符
            String channelName = "群聊消息通知";
            String channelDescription = "来自群聊的消息";
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription(channelDescription);
            manager.createNotificationChannel(notificationChannel);
        }
        startForegroundService(new Intent(StartActivity.this, NettyService.class));
    }

    @Subscribe
    public void onMessage(OtherMessage message) {
        switch (message.getEvent()) {
            case Constants.CONNECTION_ERROR -> {
                AppExecutor.getInstance().MAIN_HANDLER().post(()-> AlertUtil.show(StartActivity.this, "网络错误", "服务器连接失败", b -> finish()));
            }
            case Constants.CONNECTION_SUCCESS -> {
                startActivity(new Intent(StartActivity.this, LoginActivity.class));
                finish();
            }
        }
    }

}